package runnable;

import impl.ServerImpl;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.util.Date;
import java.util.Scanner;

import dao.UserCharacter;

public class ServerLaunch {

	public static void main(String[] args) throws Exception {
		
		ServerImpl remoteServer = new ServerImpl();
		ServerImpl remoteServer2 = new ServerImpl();

		LocateRegistry.createRegistry(1099);
		Naming.rebind("Dungeon", remoteServer);
		System.out.println("Server1 has been started "+new Date().toString());
		
		LocateRegistry.createRegistry(1098);
		Naming.rebind("Dungeon2", remoteServer);
		System.out.println("Server2 has been started "+new Date().toString());
		
		//TODO log the server start
		
		try {
			remoteServer.createDungeon();
			remoteServer2.setRemoteUsers(remoteServer.getRemoteUsers());
			remoteServer2.setUsers(remoteServer.getUsers());
			remoteServer2.setMyDungeon(remoteServer.getMyDungeon());
			System.out.println("Server has been loaded "+new Date().toString());
			System.out.println("For save the Dungeon state, type \"save\", and press enter");
			System.out.println("For killing the Dungeon, type \"quit\", and press enter");
			System.out.println("For bannish a character, type \"ban\" followed by the Pseudo, and press enter");
			System.out.println("For move a character, type \"move\" followed by the Pseudo and the room ID, and press enter");
		} catch (Exception e) {
			System.out.println("Server can't be loaded "+new Date().toString());
			e.printStackTrace();
		}
		
		remoteServer.manageMonsters();
		
		remoteServer.reloadAllCharacters();
		
		String line;
	    Scanner in = new Scanner(System.in);
	    while (in.hasNextLine()) {
	    	line = in.nextLine();
	    	if (line.equals("save")) {
	    		remoteServer.saveDungeon();
	    	} else if (line.equals("quit")) {
	    		System.exit(0);
	    	} else {
	    		// commands
	    		String[] command = line.split(" ");
	    		if (command[0].equals("ban")) {
	    			String name = command[1];
	    			if (remoteServer.getMyDungeon().people.containsKey(name)) {
	    				UserCharacter uc = remoteServer.getMyDungeon().people.get(name);
	    				uc.setCurrentRoom(remoteServer.getMyDungeon().rooms.get(0));
	    				String login = uc.getOwner().getLogin();
	    				if (remoteServer.getRemoteUsers().get(login) != null) {
	    					remoteServer.getRemoteUsers().get(login).notifyMessage("You have been bannished by the administrator");
	    					remoteServer.getRemoteUsers().get(login).majRoom();
	    				}
	    			}
	    		}
	    		if (command[0].equals("move")) {
	    			String name = command[1];
	    			int idroom = Integer.parseInt(command[2]);
	    			if (remoteServer.getMyDungeon().people.containsKey(name)) {
	    				UserCharacter uc = remoteServer.getMyDungeon().people.get(name);
	    				uc.setCurrentRoom(remoteServer.getMyDungeon().rooms.get(idroom));
	    				String login = uc.getOwner().getLogin();
	    				if (remoteServer.getRemoteUsers().get(login) != null) {
	    					remoteServer.getRemoteUsers().get(login).notifyMessage("You have been moved to room "+idroom+" by the administrator");
	    					remoteServer.getRemoteUsers().get(login).majRoom();
	    				}
	    			}
	    		}
	    	}
	    }
		
		
	}
	
}
